﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

namespace Deflection
{
    class ProjectileFactory
    {
        public enum ProjectileFactoryState
        {

        }

        public static SoundEffect Explode1 { get; set; }
        public static SoundEffect Explode2 { get; set; }
        public static SoundEffect Explode3 { get; set; }
        private static ProjectileFactory instance = null;

        public List<GameObject> projectiles = new List<GameObject>();

        private Texture2D ProjectileSprite { get; set; }

        
        public static ProjectileFactory Instance()
        {
            if (instance == null)
            {
                instance = new ProjectileFactory();
            }
            return instance;
        }

        private ProjectileFactory()
        {

        }

        public void Update(GameTime gameTime)
        {
            for (int i = 0; i < projectiles.Count; i++)
            {
                projectiles[i].Update(gameTime);
                if (projectiles[i].state == GameObject.ObjectState.Alive)
                {
                    CheckCollisions(projectiles[i]);
                }
            }
            projectiles.RemoveAll(FindDeadProjectiles);

        }
        private bool FindDeadProjectiles(GameObject e)
        {
            if (!e.Alive)
            {
                return true;
            }
            return false;
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            foreach (GameObject projectile in projectiles)
            {
                if (projectile.Alive)
                {
                    projectile.Draw(gameTime, spriteBatch);
                }
            }
        }

        /// <summary>
        /// Loads all the content, sprites etc for all the projectiles. Make sure this is called before any projectile is instanced
        /// </summary>
        /// <param name="content"></param>
        public void LoadContent(ContentManager content)
        {
            // Load all projectile sprites
            Missile.LoadContent(content, "Sprites/EnemyDummy");
            Explode1 = content.Load<SoundEffect>("Audio/Effects/Explode");
            Explode2 = content.Load<SoundEffect>("Audio/Effects/Explode2");
            Explode3 = content.Load<SoundEffect>("Audio/Effects/Explode3");

            // Laser.LoadContent(content, "Laser");
            // etc
        }

        public static Projectile CreateProjectile(Vector2 position, Projectile.ProjectileType projectileType, Vector2 direction, Color color, Player player)
        {
            Projectile projectile = null;
            switch (projectileType)
            {
                case Projectile.ProjectileType.Missile:
                    projectile = new Missile(position, direction);
                    break;
                case Projectile.ProjectileType.PlayerBasicProjectile:
                    projectile = new PlayerBasicProjectile(position, direction, color, player);
                    break;
                default:
                    break;
            }
            // Add the new projectile to the list of projectiles
            instance.projectiles.Add(projectile);
            
            return projectile;
        }

        public void CheckCollisions(GameObject projectile)
        {
            List<Enemy> enemies = EnemyFactory.Instance().Enemies;

            for ( int e = 0; e < enemies.Count; e++)
            {
                if (enemies[e].Type != Enemy.EnemyType.SuperSquare && enemies[e].Type != Enemy.EnemyType.SuperTriangle && enemies[e].Type != Enemy.EnemyType.SuperHexagon && enemies[e].IsCollidable)
                {
                    if (projectile.CheckForCollision(enemies[e]))
                    {
                        projectile.state = GameObject.ObjectState.Dying;
                        enemies[e].State = Enemy.EnemyState.Dying;
                        Random rand = new Random((int)enemies[e].position.X);
                        int r = rand.Next(0, 100);
                        if (r <= 33)
                        {
                            Explode1.Play(0.125f, 1.0f, 0.0f);
    
                        }
                        else if (r <= 66)
                        {
                            Explode3.Play(0.125f, 1.0f, 0.0f);
                        }
                        else
                        {
                            Explode2.Play(0.125f, -0.50f, 0.0f);
                        }
                        enemies[e].killer = projectile.player;
                        if (enemies[e].killer != null)
                        {
                            enemies[e].killer.Score.AddEnemyDeath(enemies[e].Type);
                        }
                    }
                    
                }
            } 
        }


        
    }
}
